"""完全搜索 API Router"""
from typing_extensions import Annotated

from fastapi import APIRouter, Depends, Query

from lib.common import get_paging_info
from api.v1.models.pagination import PagenationRequest
from api.v1.models.response import response_401, response_403, response_422
from api.v1.models.board import ResponseSearchModel
from service.search import SearchServiceAPI

router = APIRouter()


@router.get("/search",
            summary="搜索公告栏",
            responses={**response_401, **response_403, **response_422}
            )
async def api_search(
    service: Annotated[SearchServiceAPI, Depends(SearchServiceAPI.async_init)],
    pagination: Annotated[PagenationRequest, Depends()],
    sfl: str = Query("wr_subject||wr_content", title="搜索字段", description="搜索字段"),
    stx: str = Query(..., title="搜索词", description="搜索词"),
    sop: str = Query("and", title="按 ------------------------------------------------------------------------------ 搜索", description="按 ------------------------------------------------------------------------------ 搜索", pattern="and|or"),
    onetable: str = Query(None, title="集成搜索", description="集成搜索"),
) -> ResponseSearchModel:
    """
搜索公告栏
-搜索公告栏类型和各个公告栏中的帖子。
    """
    boards = service.get_boards()
    page = pagination.page
    per_page = pagination.per_page
    searched_result = service.search(boards, sfl, stx, sop, page, per_page)
    total_search_count = searched_result["total_search_count"]
    boards = searched_result["boards"]
    paging_info = get_paging_info(page, per_page, total_search_count)

    return {
        "total_records": total_search_count,
        "total_pages": paging_info["total_pages"],
        "onetable": onetable,
        "boards": boards,
    }
